package com.moorwind.chartservice.utils
{
	import com.moorwind.chartservice.events.TokenEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	public class TokenUtil extends EventDispatcher
	{
		
		public function getToken(url:String):void
		{
			var loader:URLLoader = new URLLoader();
			//var param:URLVariables = new URLVariables();
			
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.GET;
			//request.data = param;
			
			loader.addEventListener(Event.COMPLETE, onData_handler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError_handler);
			loader.load(request);
		}
		
		private function onData_handler(e:Event):void
		{
			var xml:XML = new XML(e.target.data);			
			var evt:TokenEvent = new TokenEvent(TokenEvent.TOKEN_KEY);
			evt.token = String(xml);
			dispatchEvent(evt);
		}
		
		private function onError_handler(e:Event):void
		{
			dispatchEvent(new TokenEvent(TokenEvent.TOKEN_ERROR));
		}

	}
}